import axios from 'axios'
import { ElMessage } from 'element-plus'

let HTTP = axios.create({
  baseURL: '/',
  timeout: 10000,
  responseType: 'json',
  // header:{  //自定义请求头
  'custom-header': 'xiao',
  //   'content-type':'application/x-www-form-urlencoded'
  // },
  params: {
    //TOKEN: localStorage['TOKEN']
  },
  transformResponse: [function (data) {
    return JSON.parse(data);
  }]
});

//添加一个请求拦截器：请求之前的拦截
HTTP.interceptors.request.use(function (config) {
  config.headers.TOKEN = localStorage['TOKEN'];
  return config;
}, function (error) {
  return Promise.reject(error);
});

//添加一个请求拦截器：请求之后的拦截
HTTP.interceptors.response.use(res => {
  return res.data.data;
}, error => {
  ElMessage.error({
    title: "错误",
    message: error.message,
  });
})

export default HTTP;